<?php

class Entry_model extends MY_Model {

    public function __construct() {
        parent::__construct();

        $this->table = 'jd_entry';
    }

    public function records($limit,$offset,$conditions) {
        $this->db->select('e.*,p.file_name as cover_name');
        
        $this->db->from($this->table.' e');
 
        $this->db->join('jd_album al','e.album=al.id','left');
        $this->db->join('jd_photo p','al.cover=p.id','left');
        
        if(isset($conditions[0])){
            foreach($conditions as $condition){
                $this->db->where('e.'.$condition[0],$condition[1]);
            }
        }
        
        if($limit){
            $this->db->limit($limit,$offset);
        }
        
        $this->db->order_by('e.created_at','desc');
        
        $query = $this->db->get();
        
        return $query->result();
    }
    
    public function total($conditions){
        $this->db->from($this->table.' e');
 
        $this->db->join('jd_album al','e.album=al.id','left');
        $this->db->join('jd_photo p','al.cover=p.id','left');
        
        if(isset($conditions[0])){
            foreach($conditions as $condition){
                $this->db->where($condition[0],$condition[1]);
            }
        }
        return $this->db->count_all_results();
    }

}